package com.wbao.dianniu.citylist;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PingYinUtil {

	/**
	 * ���ַ����е�����ת��Ϊƴ��,�����ַ�����
	 */
	public static String getPingYin(String inputString) {
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		format.setVCharType(HanyuPinyinVCharType.WITH_V);

		Pattern p = Pattern.compile("^[\u4E00-\u9FA5A-Za-z_]+$");
		if(inputString.length() > 0){
			Matcher matcher = p.matcher(inputString.substring(0, 1));
			if (matcher.find()) {
				char[] input = inputString.trim().toCharArray();
				String output = "";
				try {
					for (int i = 0; i < input.length; i++) {
						if (java.lang.Character.toString(input[i]).matches(
								"[\\u4E00-\\u9FA5]+")) {
							String[] temp = PinyinHelper.toHanyuPinyinStringArray(
									input[i], format);
							output += temp[0];
						} else
							output += java.lang.Character.toString(input[i]);
					}
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
				return output;
			} else {
				return "";
			}
		}else{
			return "";
		}
		
	}

	public static String converterToFirstSpell(String chines) {
		String pinyinName = "";
		char[] nameChar = chines.toCharArray();
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		for (int i = 0; i < nameChar.length; i++) {
			if (nameChar[i] > 128) {
				try {
					pinyinName += PinyinHelper.toHanyuPinyinStringArray(
							nameChar[i], defaultFormat)[0].charAt(0);
				} catch (BadHanyuPinyinOutputFormatCombination e) {
					e.printStackTrace();
				}
			} else {
				pinyinName += nameChar[i];
			}
		}
		return pinyinName;
	}
}